package titt.service.invoke;

import titt.service.ThreadSafe;
import titt.type.TypedObject;

public class ThreadSafeInvoker implements MethodInvoker {

	private final MethodInvoker mi;
	private final boolean ts;
	private final Object o;
	
	public ThreadSafeInvoker(Object o, MethodInvoker mi) {
		this.mi = mi;
		this.o = o;
		ts = o instanceof ThreadSafe;
	}
	
	@Override
	public TypedObject call(String name, TypedObject... args) throws Exception {
		if(ts)
			return mi.call(name, args);
		synchronized (o) {
			return mi.call(name, args);
		}
	}

}
